<?php
class Emails extends MY_Controller 
{
	var $curl = 'emails';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $id_email = '';
		
	function __construct()
	{
		parent::__construct();
		
		$this->load->model(array('/public/email_model'));

		$this->validation->set_fields($this->email_model->fields);
		
		$this->data['Title'] = 'Email Templates';		

		$this->language = parent::getLang();

		$this->name = T_('Email Templates');
	}
	
	function index()
	{
		$this->email_model->search();
		
		$this->renderLayout();
	}

	function create()
	{		
		if (count($_POST))
		{
			$rules = array('subject', 'name', 'mailtype', 'text');
			$this->validation->set_rules($this->email_model->getRules($rules));

			$this->validation->set_message('__nameUnique', '<span class="black bold">«'.T_("Email Template").'»</span> '.T_("already exists with such ID. Please change the ID."));	

			if ($this->validation->run() === TRUE)
			{				
				$item['name'] 				= $this->validation->name;
				$item['subject'] 			= $this->validation->subject;
				$item['message'] 			= $this->validation->text;				
				$item['mailtype'] 			= $this->validation->mailtype;
						
				if ($this->email_model->insert($item))
				{
					redirect($this->curl . '/index');	
				}				
			}
		}
				
		$this->data['Title'] = 'Create Email Template';
		$this->data['wysiwyg'] = true;
		
		$this->renderLayout();			
	}
	
	function edit($id = null)
	{
		if (!empty($id))
		{
			$object = $this->email_model->get($id);

			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
						
			if (!count($_POST))
			{
				$this->validation->id 		= $object['id'];
				$this->validation->name 	= $object['name'];
				$this->validation->subject 	= $object['subject'];
				$this->validation->text 	= $object['message'];
				$this->validation->mailtype = $object['mailtype'];				 
			}
			else
			{
				$this->id_email = $id;
				
				$rules = array('subject', /*'name',*/ 'mailtype', 'text');
				$this->validation->set_rules($this->email_model->getRules($rules));
	
      			$this->validation->set_message('__nameUnique', '<span class="black bold">«'.T_("Email Template").'»</span> '.T_("already exists with such ID. Please change the ID."));
      				
				if ($this->validation->run() === TRUE)
				{					
					$item['id']		 			= $this->validation->id;
					//$item['name'] 			= $this->validation->name;
					$item['subject'] 			= $this->validation->subject;
					$item['message'] 			= $this->validation->text;				
					$item['mailtype'] 			= $this->validation->mailtype;
					
					$this->email_model->update($item);
					
					redirect($this->curl . '/index');
				}
			}
		}
		
		$this->data['Title'] .= ' / Edit';
		$this->data['wysiwyg'] = true;
		
		$this->renderLayout();
	}
		
	function deleteItems()
	{
		$data = $this->email_model->delete($_REQUEST['items']);

		echo $data;
	}

	function delete($id)
	{
		$data = $this->email_model->delete($id);

		redirect($this->curl . '/index');
	}
		
	function __nameUnique($name)
	{		
		return $this->email_model->nameUnique($name);
	}
}
?>